package com.simple.util.math.function;

/**
 * @program: simple_tools
 * @description: 指数函数
 * @author: ChenWenLong
 * @create: 2019-10-25 09:51
 **/
public class ExponentialFunction {

    //y=a^x函数(a为常数且以a>0，a≠1)
    private double a = 2;
    public static ExponentialFunction instance;
    private static final Point DEFAULT_POINT = new Point(0,1);
    private ExponentialFunction(){}

    static {
        if(instance == null){
            synchronized (ExponentialFunction.class){
                if(instance == null){
                    instance = new ExponentialFunction();
                }
            }
        }
    }

    /**
     * 功能描述:
     * 〈创建一个指数函数〉
     *
     * @params : [a]
     * @return : void
     * @author : cwl
     * @date : 2019/10/25 10:11
     */
    public static void init(double a){
        if(a < 0 || a == 1){
            throw new RuntimeException("a is not less than zero and not be one");
        }
        instance.setA(a);
    }

    /**
     * 功能描述:
     * 〈根据Y获取X的值〉
     *
     * @params : [y]
     * @return : double
     * @author : cwl
     * @date : 2019/10/25 10:15
     */
    public static double getX(double y,int result){
        double a = instance.getA();
        double nextTime;
        if(y % a == 0 && y % a != 1){
            nextTime = y / a;
            result++;
            getX(nextTime,result);
        }
        if(y % a == 1){
            return result;
        }
        throw new RuntimeException("this y is not on line");
    }

    /**
     * 功能描述:
     * 〈根据x获取y的值〉
     *
     * @params : [x]
     * @return : double
     * @author : cwl
     * @date : 2019/10/25 10:31
     */
    public static double getY(double x){
        double a = instance.getA();
        return Math.pow(x,a);
    }

    /**
     * 功能描述:
     * 〈获取指数函数默认经过的点〉
     *
     * @params : []
     * @return : com.simple.util.math.function.ExponentialFunction.Point
     * @author : cwl
     * @date : 2019/10/25 10:32
     */
    public static Point getDefaultPoint(){
        return DEFAULT_POINT;
    }

    /**
     * 功能描述:
     * 〈判断点是否在指数函数上〉
     *
     * @params : [point]
     * @return : boolean
     * @author : cwl
     * @date : 2019/10/25 10:33
     */
    public static boolean isOnline(Point point){
        double x = point.getX();
        double y = point.getY();
        return y == Math.pow(x,instance.getA());
    }

    //坐标点
    public static class Point {
        //坐标x
        private double x;
        //坐标y
        private double y;

        public Point(double x,double y){
            this.x = x;
            this.y = y;
        }
        public double getX() {
            return x;
        }

        public void setX(double x) {
            this.x = x;
        }

        public double getY() {
            return y;
        }

        public void setY(double y) {
            this.y = y;
        }

        @Override
        public String toString() {
            return "Point{" +
                    "x=" + x +
                    ", y=" + y +
                    '}';
        }
    }

    public double getA() {
        return a;
    }

    public void setA(double a) {
        this.a = a;
    }
}
